package com.eqdd.yiqidian.ui.act.tree

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.eqdd.yiqidian.R
import com.eqdd.yiqidian.base.Config
import com.eqdd.yiqidian.bean.TypeTreeBean
import com.jhj.datalibrary.tree.single.SimpleSingleListAdapter
import kotlinx.android.synthetic.main.layout_type_tree_node.view.*

/**
 * Created by jhj on 18-9-29.
 */
class ActTypeListAdapter(val mContext: Context) : SimpleSingleListAdapter<TypeTreeBean, ActTypeListAdapter.ItemViewHolder>() {


    override fun onBindItemViewHolder(holder: ItemViewHolder, data: TypeTreeBean, position: Int) {
        holder.itemView.tv_name.text = data.name
    }

    override fun onCreateItemView(parent: ViewGroup?, viewType: Int): View {
        val inflater = LayoutInflater.from(parent?.context)
        return inflater.inflate(R.layout.layout_type_tree_node, parent, false)
    }

    override fun onCreateItemViewHolder(view: View): ItemViewHolder {
        return ItemViewHolder(view)
    }

    inner class ItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        init {
            itemView.setOnClickListener {
                val data = itemView.tag as TypeTreeBean
                itemViewOnClick(data)
                if (!data.isRoot) {
                    if (mContext is Activity) {
                        val intent = Intent()
                        intent.putExtra(Config.NAME, data.name)
                        mContext.setResult(Activity.RESULT_OK, intent)
                        mContext.finish()
                    }
                }
            }
        }
    }
}